import 'package:flutter/material.dart';

enum ReminderType {
  feeding, // 喂奶提醒
  medicine, // 用药提醒
  vaccine, // 疫苗提醒
  checkup, // 体检提醒
  custom, // 自定义提醒
}

extension ReminderTypeExtension on ReminderType {
  IconData get icon {
    switch (this) {
      case ReminderType.feeding:
        return Icons.baby_changing_station;
      case ReminderType.medicine:
        return Icons.medication;
      case ReminderType.vaccine:
        return Icons.vaccines;
      case ReminderType.checkup:
        return Icons.local_hospital;
      case ReminderType.custom:
        return Icons.notifications;
    }
  }

  String get label {
    switch (this) {
      case ReminderType.feeding:
        return '喂奶';
      case ReminderType.medicine:
        return '用药';
      case ReminderType.vaccine:
        return '疫苗';
      case ReminderType.checkup:
        return '体检';
      case ReminderType.custom:
        return '自定义';
    }
  }
}

class Reminder {
  final String? id; // Changed from int to String? to match SQLite
  final String babyId;
  final String title;
  final ReminderType type;
  final TimeOfDay scheduleTime; // Changed from DateTime to TimeOfDay
  final bool repeat;
  final String? notes; // Added notes field
  final bool isActive;

  Reminder({
    this.id,
    required this.babyId,
    required this.title,
    required this.type,
    required this.scheduleTime,
    this.repeat = false,
    this.notes,
    this.isActive = true,
  });

  factory Reminder.fromMap(Map<String, dynamic> map) {
    final timeString = map['schedule_time'] as String;
    final timeParts = timeString.split(':');

    return Reminder(
      id: map['id']?.toString(),
      babyId: map['baby_id'] as String,
      title: map['title'] as String,
      type: ReminderType.values.firstWhere(
        (type) => type.toString() == map['type'],
        orElse: () => ReminderType.custom,
      ),
      scheduleTime: TimeOfDay(
        hour: int.parse(timeParts[0]),
        minute: int.parse(timeParts[1]),
      ),
      repeat: (map['repeat'] as int) == 1,
      notes: map['notes'] as String?,
      isActive: (map['is_active'] as int) == 1,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      if (id != null) 'id': id,
      'baby_id': babyId,
      'title': title,
      'type': type.toString(),
      'schedule_time': '${scheduleTime.hour}:${scheduleTime.minute}',
      'repeat': repeat ? 1 : 0,
      'notes': notes,
      'is_active': isActive ? 1 : 0,
    };
  }

  Reminder copyWith({
    String? title,
    TimeOfDay? scheduleTime,
    bool? repeat,
    String? notes,
    bool? isActive,
  }) {
    return Reminder(
      id: this.id,
      babyId: this.babyId,
      title: title ?? this.title,
      type: this.type,
      scheduleTime: scheduleTime ?? this.scheduleTime,
      repeat: repeat ?? this.repeat,
      notes: notes ?? this.notes,
      isActive: isActive ?? this.isActive,
    );
  }

  // Helper method to get next reminder time
  DateTime? getNextReminder() {
    final now = DateTime.now();
    final todaySchedule = DateTime(
      now.year,
      now.month,
      now.day,
      scheduleTime.hour,
      scheduleTime.minute,
    );

    if (todaySchedule.isAfter(now)) {
      return todaySchedule;
    } else if (repeat) {
      return todaySchedule.add(const Duration(days: 1));
    }

    return null;
  }

  bool get isExpired {
    final now = DateTime.now();
    final todaySchedule = DateTime(
      now.year,
      now.month,
      now.day,
      scheduleTime.hour,
      scheduleTime.minute,
    );
    return !repeat && todaySchedule.isBefore(now);
  }
}
